package com.ahj.location;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qingning.qn.R;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.help.Tip;

import java.util.ArrayList;

public class AdapterSearch extends BaseAdapter {
    private Context context;
    private ArrayList list;
    private boolean isAuto;//是否自动提示

    private int selectIndex = 0;

    public void reset() {
        setSelectIndex(0);
    }

    public PoiItem getSelectItem() {
        return (PoiItem) list.get(selectIndex);
    }

    public void setSelectIndex(int index) {
        selectIndex = index;
        notifyDataSetChanged();
    }

    public AdapterSearch(Context context, ArrayList list, boolean isAuto) {
        this.context = context;
        this.list = list;
        this.isAuto = isAuto;
    }

    @Override
    public int getCount() {
        if (list == null)
            return 0;
        else
            return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search, null);
            holder.tvTitle = convertView.findViewById(R.id.tvTitle);
            holder.tvSubTitle = convertView.findViewById(R.id.tvSubTitle);
            holder.imgCheck = convertView.findViewById(R.id.imgCheck);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (selectIndex == position) {
            holder.tvTitle.setTextColor(context.getResources().getColor(R.color.alivc_player_theme_blue));
            holder.imgCheck.setVisibility(View.VISIBLE);
        } else {
            holder.tvTitle.setTextColor(context.getResources().getColor(R.color.per87Black));
            holder.imgCheck.setVisibility(View.INVISIBLE);
        }

        if (!isAuto) {
            PoiItem item = (PoiItem) list.get(position);
            holder.tvTitle.setText(item.getTitle());
            holder.tvSubTitle.setText(item.getSnippet());
        } else {
            Tip tip = (Tip) list.get(position);
            holder.tvSubTitle.setText(tip.getName());
            holder.tvTitle.setVisibility(View.GONE);
            holder.imgCheck.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder {
        TextView tvTitle, tvSubTitle;
        ImageView imgCheck;
    }
}
